let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    query: {
      cstore_id:null,
      coupon_member_kwd: "",
      clerk_member_kwd: "",
      order_sn: "",
      goods_name: "",
      start_time: "",
      end_time: "",
    },
    isLoadMore: true,
    page:1,
    totalPage:0,
    examineList:[],
    startShow:false, //开始时间弹窗
    endShow: false, //结束时间弹窗
    currentDate: new Date().getTime(),
    terminateDate: new Date().getTime(),
    minDate: new Date().getTime(),
    startDate: '',
    endDate: '',
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    let dateTime = new Date().getFullYear(); /* 获取现在的年份 */
   
    let minDate = new Date(new Date().setFullYear(dateTime-10)).getTime();
    console.log(minDate);
    console.log(dateTime);
    if((options.storeId??'') !== '') {
      this.setData({
        'query.cstore_id': parseInt(options.storeId),
        minDate: minDate
      })
      this.examineList();
    }
  },
  startTimeOn() {
    this.setData({
      startShow: true
    })
  },
  endTimeOn() {
    this.setData({
      endShow: true
    })
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  modelInput(event) {
    const item = event.currentTarget.dataset.name,
          itemValue = event.detail;
    this.setData({
      [item]:itemValue
    })
  },
  onSearch() {
    console.log(this.data.query);
    this.examineList();
  },
  onClose(event) {
    let type = event.currentTarget.dataset.state;
    if(type === 'start') {
      this.setData({ 
        startShow: false, 
        startDate: '',
        'query.start_time': ''

      });
    } else {
      this.setData({ 
        endShow: false,
        endDate: '',
        'query.end_time': ''

      });
    }
  },
  onConfirm(event) {
    let type = event.currentTarget.dataset.state;
    // console.log(event);
    let setTime = Math.round(new Date(event.detail).getTime() / 1000);
    let endTime = new Date(new Date(new Date(event.detail).toLocaleDateString()).getTime() +24*60*60*1000-1) / 1000;
    if(type === 'start') {
      this.setData({ 
        startShow: false,
        startDate: this.formatDate(event.detail),
        'query.start_time': setTime
      });
    } else {
      this.setData({ 
        endShow: false,
        endDate: this.formatDate(event.detail),
        'query.end_time': parseInt(endTime)
      });
    }
  },
  examineList() { //核销明细列表
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.store-manage.examineList");
    let json = this.data.query;
    json.page = this.data.page;
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.totalPage = res.data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            examineList: res.data.data
          })
        } else {
          this.data.page = this.data.page - 1;
          this.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.store-manage.examineList");
      let json = this.data.query;
      json.page = this.data.page;
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              examineList : this.data.examineList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      page:1
    })
    this.examineList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})